package com.shinektv.lrclib;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class LrcTextView extends RelativeLayout {
    //字幕描边宽度
    public static final int STROKE_WIDTH = 2;
    //字幕阴影半径
    public static final int SHADOW_LAYER_RADIUS = 1;

    //未唱的
    private StrokeView unSungView;
    //已唱的
    private StrokeView songView;
    private String lrc = "";

    public String getLrc() {
        return lrc;
    }

    public LrcTextView(Context context) {
        this(context, null);
    }

    public LrcTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LrcTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        unSungView = new StrokeView(context);
        unSungView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        unSungView.setId(R.id.default_textview_id);
        unSungView.setTextColor(Color.WHITE);
        unSungView.setStrokeColor(Color.BLACK);

        songView = new StrokeView(context);
        songView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        songView.setTextColor(Color.BLUE);
        songView.setStrokeColor(Color.WHITE);

        addView(unSungView);
        addView(songView);
        songView.setWidth(0);
    }

    //设置未唱歌词颜色
    public void setDefaultColor(int color) {
        unSungView.setTextColor(color);
    }

    //设置已唱歌词颜色
    public void setSelectColor(int color) {
        songView.setTextColor(color);
    }

    public void setPercentage(float percentage) {
        setSelectWidth((int) (getTextWidth() * percentage));
    }

    public void setLrc(String lrc) {
        this.lrc = lrc;
        unSungView.setLrc(lrc);

        songView.setLrc(lrc);
        songView.setWidth(0);
    }

    private int getTextWidth() {
        return unSungView.getWidth();
    }

    public void setSelectWidth(int width) {
        if (width <= getTextWidth()) {
            width += 5;
            songView.setWidth(width);
        }
    }

    //控制歌词居右
    public void alignWithParent() {
        LayoutParams params = (LayoutParams) unSungView.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        LayoutParams params2 = (LayoutParams) songView.getLayoutParams();
        params2.addRule(RelativeLayout.ALIGN_LEFT, unSungView.getId());
    }
}
